/**
 * 详情对话框
 */
var HomeConfigInfoDlg = {
    data: {
        id: "",
        pic: "",
        tz: "",
        path: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;




    //普通图片上传
    var upload = layui.upload;
    upload.render({
        elem: '#pic'
        ,url: Feng.ctxPath + '/system/uploadFtp' //改成您自己的上传接口
        ,done: function(res){
            $('#filePreview').attr('src', res.data); //图片链接（base64）
            $('#picval').val(res.data)
            Feng.success("上传成功！");
        }
        ,error: function(){
            Feng.error("上传文件失败！");
        }
    });



    form.on("select(tz)", function (data) {
        console.log(data.value);

        var html = ""

        if(data.value == 2){
            var ajax = new $ax(Feng.ctxPath + "/redline/listall");
            var result = ajax.start();
            var list = result.data
            html += "<select id='path' name='path'>"
            for(var i=0;i<list.length;i++){
                html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>";
            }
            html += "</select>"
            $("#dt").attr("style","display:block;")
            $("#tplhtml").html(html)
            form.render();
        }else if(data.value == 3){
            var html = '<input id="path" name="path" placeholder="请输入Url地址" type="text" class="layui-input" lay-verify="required" required/>'
            $("#dt").attr("style","display:block;")
            $("#tplhtml").html(html)
            form.render();
        }else {
            $("#dt").attr("style","display:none;")
            $("#tplhtml").html("")
            form.render();
        }
    })






    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/homeConfig/detail");
    var result = ajax.start();

    if(result.data.pic!=null && result.data.pic.length > 0){
        $('#filePreview').attr('src', result.data.pic); //图片链接（base64）
    }
    form.val('homeConfigForm', result.data);
    if(result.data.tz == 2){
        var ajax = new $ax(Feng.ctxPath + "/redline/listall");
        var res = ajax.start();
        var list = res.data
        html = "<select id='path' name='path'>"
        for(var i=0;i<list.length;i++){
            html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>";
        }
        html += "</select>"
        $("#dt").attr("style","display:block;")
        $("#tplhtml").html(html)
        form.render();
    }else if(result.data.tz == 3){
        var html = '<input id="path" name="path" value="'+result.data.path+'" placeholder="请输入Url地址" type="text" class="layui-input" lay-verify="required" required/>'
        $("#dt").attr("style","display:block;")
        $("#tplhtml").html(html)
        form.render();
    }else {
        $("#dt").attr("style","display:none;")
        $("#tplhtml").html("")
        form.render();
    }

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/homeConfig/editItem", function (data) {
            Feng.success("保存成功！");
            window.location.href = Feng.ctxPath + '/homeConfig'
        }, function (data) {
            Feng.error("保存失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    $('#cancel').click(function(){
        window.location.href = Feng.ctxPath + '/homeConfig'
    });
});